package com.lp.controller.iot;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lp.bo.IotSceneInfoBO;
import com.lp.bo.IotSensorInfoBO;
import com.lp.bo.IotVideoInfoBO;
import com.lp.bo.UserInfoBO;
import com.lp.bo.VideoFileInfoBO;
import com.lp.cache.CacheName;
import com.lp.cache.ProCacheUtil;
import com.lp.common.Code;
import com.lp.common.CodeIot;
import com.lp.common.Constants;
import com.lp.controller.BaseController;
import com.lp.service.IotOtherInfoService;
import com.lp.util.CommonUtil;
import com.lp.util.ObjectUtil;
import com.lp.util.PageBean;


@Controller
public class IotWebViewController extends BaseController {
	
	
	private final static String VIEW_PATH2 = "/oss/piot/" ;
	
	@RequestMapping(method = RequestMethod.GET, value = "/piot/{viewName}")
	public String ossPage2(HttpServletRequest req, HttpServletResponse resp,
			@PathVariable String viewName) {
		setModelWeb(req,viewName);
		return VIEW_PATH2 + viewName;
	}
	
	private void setModelWeb(HttpServletRequest req , String viewName){
		if(viewName.contains("index")){
			// 检索当前用户下看到场景
			UserInfoBO userInfo = getUserInfoBySession(req);
			IotSceneInfoBO obj =new IotSceneInfoBO();
			if( ObjectUtil.isNotEmpty(userInfo)){
				if(  userInfo.getType() != Code.UserType.SUPER+0){
					obj.setUser_id(userInfo.getId());
				}
				req.setAttribute("sceneInfo", getData(service.selectList("IotSceneInfo.selects", obj)) );				
			}
		}
	}
	
	
	private final static String VIEW_PATH = "/oss/iot/" ;
	
	@RequestMapping(method = RequestMethod.GET, value = "/iot/{viewName}")
	public String ossPage(HttpServletRequest req, HttpServletResponse resp,
			@PathVariable String viewName) {
		return VIEW_PATH + viewName;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/iot/scene/{viewName}")
	public ModelAndView ossScenePage(HttpServletRequest req, HttpServletResponse resp,
			@PathVariable String viewName) {
		Map<String,Object> resultData = getResultMap();
		try{
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH + viewName,"info");
	}
	
	private final static String VIEW_PATH3 = "/sample/" ;
	
	@RequestMapping(method = RequestMethod.GET, value = "/sample/{viewName}")
	public String samplePage(HttpServletRequest req, HttpServletResponse resp,
			@PathVariable String viewName) {
		return VIEW_PATH3 + viewName;
	}
	
	@Autowired
	IotOtherInfoService IotOtherInfoService ;
	
	/**
	 *  /  
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/")
	public String firstIndex(HttpServletRequest req, HttpServletResponse resp){
		Map<String,Object> resultData = getResultMap();
		try{
			if(CommonUtil.checkReqUtil.checkAgentIsMobile(req)){
				return "/wechat/iot/login" ;
			}
			return "/oss/iot/login" ;
			
		}catch(Exception e){
			exception(e,resultData);
		}
		return null;
	}
	
	
	/**
	 * 项目信息统计
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/app_status")
	public ModelAndView appStatusPage(HttpServletRequest req, HttpServletResponse resp){
		Map<String,Object> resultData = getResultMap();
		try{
			UserInfoBO user =  getUserInfoBySession(req);
			// 这边需要去数据库多次检索数据
			resultData = IotOtherInfoService.getAppStatusInfo(user);
		}catch(Exception e){
			exception(e,resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"app_status","info");
	}
	
	/**
	 * 监控中心
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/monitor")
	public ModelAndView monitorPage(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required=false) Integer id){
		Map<String,Object> resultData = getResultMap();
		try{
			if(ObjectUtil.isNotEmpty(id)){
				req.setAttribute("id", id);
			}
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"monitor","info");
	}
	
	/**
	 * 视频监控平台
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/mvideo")
	public ModelAndView monitorVideoPage(HttpServletRequest req, HttpServletResponse resp){
		Map<String,Object> resultData = getResultMap();
		try{
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"mvideo","info");
	}
	
	/**
	 * 传感器详情
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/sensors_detail")
	public ModelAndView sensorDetailrPage(HttpServletRequest req, HttpServletResponse resp , 
			@RequestParam Integer sid ,@RequestParam Integer id ){
		Map<String,Object> resultData = getResultMap();
		try{
			IotSensorInfoBO obj = new IotSensorInfoBO();
			obj.setScene_id(sid);
			resultData = service.selectPageList("IotSensorInfo.selectPage",getPageBean(1,200), obj);
			req.setAttribute("sensorId", id);
			req.setAttribute("sceneId", sid);
			
			// 这边根据传感器的类型，跳转到不同的页面，如果是定位型传感器，则跳转到定位的页面中，其他需要区分局部定位和gps定位
			obj = ProCacheUtil.getCache(CacheName.SENSORINFO, id.toString());
			if(obj.getIot_sensor_type() == CodeIot.SENSOR_TYPE.POSITION ){
				if( obj.getMeasure_unit_type() == CodeIot.SENSOR_MEASURE_UNIT_TYPE.LOCALON ){
					return getModelAndView(resp, resultData,VIEW_PATH + "location_sensors_detail","info");					
				}else{
					return getModelAndView(resp, resultData,VIEW_PATH + "location_gps_sensors_detail","info");	
				}
			}
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"sensors_detail","info");
	}
	
	/**
	 * 未读报警
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/alarm_manager")
	public ModelAndView alarmManagerPage(HttpServletRequest req, HttpServletResponse resp){
		Map<String,Object> resultData = getResultMap();
		try{
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"alarm_manager","info");
	}
	/**
	 * 全部报警
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/alarm_all")
	public ModelAndView alarmAllPage(HttpServletRequest req, HttpServletResponse resp){
		Map<String,Object> resultData = getResultMap();
		try{
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"alarm_all","info");
	}
	
	/**
	 * 历史数据
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/history_sensor_data")
	public ModelAndView historySensorDataPage(HttpServletRequest req, HttpServletResponse resp){
		Map<String,Object> resultData = getResultMap();
		try{
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"history_sensor_data","info");
	}
	
	/**
	 * 历史数据图表
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/history_sensor_chart")
	public ModelAndView historySensorChartPage(HttpServletRequest req, HttpServletResponse resp){
		Map<String,Object> resultData = getResultMap();
		try{
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"history_sensor_chart","info");
	}
	
	/**
	 * 自动触发历史数据
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/history_trigger_data")
	public ModelAndView historyTriggerDataPage(HttpServletRequest req, HttpServletResponse resp){
		Map<String,Object> resultData = getResultMap();
		try{
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"history_trigger_data","info");
	}
	
	/**
	 * 网关管理
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/gateway_manager")
	public ModelAndView gatewayManagerPage(HttpServletRequest req, HttpServletResponse resp){
		Map<String,Object> resultData = getResultMap();
		try{
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"gateway_manager","info");
	}
	
	/**
	 * 设备列表
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/device_list")
	public ModelAndView deviceListPage(HttpServletRequest req, HttpServletResponse resp){
		Map<String,Object> resultData = getResultMap();
		try{
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"device_list","info");
	}
	
	/**
	 * 传感器管理
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/sensor_manager")
	public ModelAndView sensorManagerPage(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required=false) Integer id ){
		Map<String,Object> resultData = getResultMap();
		try{
			if(ObjectUtil.isNotEmpty(id)){
				req.setAttribute("id", id);
			}
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"sensor_manager","info");
	}
	
	/**
	 * 触发器管理
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/trigger_manager")
	public ModelAndView triggerManagerPage(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required=false) Integer id){
		Map<String,Object> resultData = getResultMap();
		try{
			if(ObjectUtil.isNotEmpty(id)){
				req.setAttribute("id", id);
			}
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"trigger_manager","info");
	}
	
	/**
	 * 視頻信息管理
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/video_manager")
	public ModelAndView videoManagerPage(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required=false) Integer id){
		Map<String,Object> resultData = getResultMap();
		try{
			if(ObjectUtil.isNotEmpty(id)){
				req.setAttribute("id", id);
			}
			UserInfoBO user =  getUserInfoBySession(req);
			IotSceneInfoBO obj = new IotSceneInfoBO();
			obj.setUser_id(user.getId());
			resultData = service.selectPageList("IotSceneInfo.select",getPageBean(1,200), obj);	
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"video_manager","info");
	}
	
	/**
	 * 视频详情信息
	 * @param req
	 * @param resp
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/video")
	public ModelAndView videoDetailPage(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam Integer sid ,@RequestParam Integer id){
		Map<String,Object> resultData = getResultMap();
		try{
//			UserInfoBO user =  getUserInfoBySession(req);
//			IotVideoInfoBO obj = new IotVideoInfoBO();
//			obj.setUser_id(user.getId());
//			obj.setScene_id(sid);
//			resultData = service.selectPageList("IotVideoInfo.selectPage",getPageBean(1,200), obj);	
			req.setAttribute("videoId", id);
			req.setAttribute("sceneId", sid);
//			Map<String,Object> videoFiles = getResultMap();
//			VideoFileInfoBO ofp = new VideoFileInfoBO();
//			ofp.setVideo_id(id);
//			ofp.setDelete_flag(Constants.DELETE.NO);
//			videoFiles = service.selectPageList("VideoFileInfo.selectPage", getPageBean(1,200), ofp);
//			PageBean pagebean = getData(videoFiles);
//			List<VideoFileInfoBO> imgs = (List<VideoFileInfoBO>) pagebean.getData();
//			req.setAttribute("imgs", imgs);
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData, VIEW_PATH + "video_live" ,"info");
	}
	
	/**
	 * 视频详情信息
	 * @param req
	 * @param resp
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/iot/video/back")
	public ModelAndView videoPlayBackPage(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam Integer sid ,@RequestParam Integer id){
		Map<String,Object> resultData = getResultMap();
		try{
			UserInfoBO user =  getUserInfoBySession(req);
			IotVideoInfoBO obj = new IotVideoInfoBO();
			obj.setUser_id(user.getId());
			obj.setScene_id(sid);
			resultData = service.selectPageList("IotVideoInfo.selectPage",getPageBean(1,200), obj);	
			req.setAttribute("videoId", id);
			req.setAttribute("sceneId", sid);
		}catch (Exception e) {
			exception(e, resultData);
		}
		return getModelAndView(resp, resultData,VIEW_PATH+"video_playback","info");
	}
	
}
